package com.event.component;

import com.event.annotation.ListenComponent;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author zhangbo
 * @program: event-plus
 * @ClassName InitEventComponent
 * @description: $
 * @date 2024-07-01 13:06:35
 */
@Slf4j
@Component
public class InitEventComponent {

    /**
     * todo
     * 之后写成需要配置的选项
     * 默认扫描当前目录下的所有
     */
    private static final String SCAN_PACKAGE = "com.event.use";

    @Resource
    private EventClassScanner eventClassScanner;

    @PostConstruct
    public void init() {
        //1 获取所有符合条件对象
        Map<String, Class<?>> listenerMap = eventClassScanner.scanEventAnnotation(SCAN_PACKAGE, ListenComponent.class);

        //2 将解析出来的对象注册到容器中
        listenerMap.forEach((k, v) -> {
            AnnotationUtils.getAnnotation(v, ListenComponent.class);
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(v);
            context.register(v);
            context.refresh();
        });






        log.info("listenerMap: {}", listenerMap);
    }

}
